package com.maben.vue_support;

import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.util.*;

@SpringBootApplication
@Slf4j
@RestController
public class AppApplication {
    private  List<Map<String,Object>> goods = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        final ConfigurableApplicationContext application = SpringApplication.run(AppApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        final String path = env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t"
                + "Application vue-support-Boot is running! Access URLs:\n\t"
                + "Local: \t\thttp://localhost:" + port + path + "/\n\t"
                + "External: \thttp://" + ip + ":" + port + path + "/\n"
                + "----------------------------------------------------------");
    }

    @RequestMapping("queryListChat")
    public Map<String,Object> queryListChat(){
        final Map<String,Object> result = new HashMap<>();
        if (goods.size()==0) {
            for (int i = 1; i < 15; i++) {
                final HashMap<String, Object> item = new HashMap<>();
                item.put("id", String.valueOf(i));
                item.put("name", "南瓜" + i);
                item.put("price", 130 + i + 0.1);
                item.put("desc", i + "这个瓜好吃~");
                item.put("state", i % 2 == 0);
                item.put("count", new Random().nextInt(5) + 1);
                goods.add(item);
            }
        }
        result.put("code","0");
        result.put("list", goods);
        return result;
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Map<String,Object> save(@RequestBody Map<String,Object> params){
        final Map<String,Object> result = new HashMap<>();
        final HashMap<String, Object> item = new HashMap<>();
        final String id = UUID.randomUUID().toString().replace("-", "");
        item.put("id", id);
        item.put("name", params.get("name"));
        item.put("price", 110);
        item.put("desc", params.get("desc"));
        item.put("state", 0);
        item.put("count", new Random().nextInt(5) + 1);
        goods.add(item);
        result.put("code","0");
        result.put("msg","保存成功");
        return result;
    }

    private List<Map<String,Object>> articles = new ArrayList<>();
    @GetMapping("queryArticles")
    public Map<String,Object> queryArticles(Integer currentPage,Integer pageSize){
        final Map<String,Object> result = new HashMap<>();
        final int size = articles.size();
        if (size ==0) {
            for (int i = 0; i < 100; i++) {
                final Map<String,Object> tmp = new HashMap<>();
                tmp.put("id",i);
                tmp.put("title","title***"+i);
                tmp.put("content","content======"+i);
                tmp.put("authorName","唐吉坷德");
                tmp.put("cmtCount",new Random().nextInt(10));
                tmp.put("pushDate",new DateTime().toString("yyyy-MM-dd"));
                tmp.put("authorId",i+1);
                articles.add(tmp);
            }
        }
        final int start = (currentPage - 1) * pageSize;
        if (start>size){
            result.put("code","0");
            result.put("list",new ArrayList<>());
            return result;
        }
        int end = start+pageSize;
        if (end>size){
            end=size;
        }
        final List<Map<String, Object>> list = articles.subList(start, end);
        result.put("code","0");
        result.put("list",list);
        return result;
    }
}
